﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Skewworks.Win.Motif.Glide
{
    public class Plugin : IMotifPlugin
    {

        #region Properties

        public FileHeader[] AcceptedHeaders
        {
            get
            {
                return new FileHeader[] { new FileHeader(true, "<Glide ") };
            }
        }

        public string ProjectTypeName 
        {
            get { return "Glide"; }
        }

        public Bitmap[] ControlListImages 
        {
            get
            {
                return new Bitmap[] { Resources.button, Resources.checkbox, Resources.datagrid, Resources.dropdown, Resources.image, Resources.passwordbox, Resources.progressbar,
                    Resources.radiobutton, Resources.slider, Resources.textblock, Resources.textbox };
            }
        }

        public string[] ControlListNames 
        {
            get { return new string[] { "Button", "CheckBox", "DataGrid", "Dropdown", "Image", "PasswordBox", "ProgressBar", "RadioButton", "Slider", "TextBlock", "TextBox" }; }
        }

        public IEmbeddedControl DefaultWindowControl
        {
            get { return new GlideWindow(); }
        }

        public bool ProjectChanged { get; set; }

        #endregion

        #region Public Methods

        public IEmbeddedControl ControlByTypeName(string typeName)
        {
            switch (typeName.ToLower())
            {
                case "button":
                    return new GlideButton();
                case "checkbox":
                    return new GlideCheckbox();
                case "datagrid":
                    return new GlideDataGrid();
                case "dropdown":
                    return new GlideDropdown();
                case "image":
                    return new GlideImage();
                case "passwordbox":
                    return new GlidePasswordBox();
                case "progressbar":
                    return new GlideProgressBar();
                case "radiobutton":
                    return new GlideRadioButton();
                case "slider":
                    return new GlideSlider();
                case "textblock":
                    return new GlideTextBlock();
                case "textbox":
                    return new GlideTextBox();
                default:
                    return null;

            }
        }

        public bool SaveProject(string filename, IEmbeddedControl defaultWindow)
        {
            try
            {
                string data = "<Glide Version=\"1.0.4\">\r\n";
                GlideWindow gw = (GlideWindow)defaultWindow;

                // Add Window Data
                data += "<Window Name=\"" + gw.Name + "\" Width=\"" + gw.Width + "\" Height=\"" + gw.Height + "\" BackColor=\"" + ColorToHex(gw.BackColor) + "\">\r\n";
                for (int i = 0; i < gw.Controls.Count; i++)
                {
                    if (gw.Controls[i] is GlideButton)
                        data += SaveGlideButton((GlideButton)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideCheckbox)
                        data += SaveGlideCheckbox((GlideCheckbox)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideDataGrid)
                        data += SaveGlideDataGrid((GlideDataGrid)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideDropdown)
                        data += SaveGlideDropdown((GlideDropdown)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideImage)
                        data += SaveGlideImage((GlideImage)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlidePasswordBox)
                        data += SaveGlidePasswordBox((GlidePasswordBox)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideProgressBar)
                        data += SaveGlideProgressBar((GlideProgressBar)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideRadioButton)
                        data += SaveGlideRadioButton((GlideRadioButton)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideSlider)
                        data += SaveGlideSlider((GlideSlider)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideTextBlock)
                        data += SaveGlideTextBlock((GlideTextBlock)gw.Controls[i]) + "\r\n";
                    else if (gw.Controls[i] is GlideTextBox)
                        data += SaveGlideTextBox((GlideTextBox)gw.Controls[i]) + "\r\n";
                }
                data += "</Window>\r\n</Glide>";

                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                byte[] b = Encoding.UTF8.GetBytes(data);
                fs.Write(b, 0, b.Length);
                fs.Close();
                fs.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not save file!\r\n" + ex.Message);
                return false;
            }
        }

        public bool OpenProject(string filename, int headerIndex, IEmbeddedControl defaultWindow)
        {
            GlideWindow gw = (GlideWindow)defaultWindow;
            XmlReaderSettings xrs = new XmlReaderSettings();
            XmlReader xr;

            xrs.IgnoreComments = true;
            xrs.IgnoreWhitespace = true;

            xr = XmlReader.Create(filename, xrs);
            while (xr.Read())
            {
                if (xr.IsStartElement())
                {
                    switch (xr.Name.ToLower())
                    {
                        case "button":
                            LoadButton(gw, xr);
                            break;
                        case "checkbox":
                            LoadCheckbox(gw, xr);
                            break;
                        case "datagrid":
                            LoadDataGrid(gw, xr);
                            break;
                        case "dropdown":
                            LoadDropdown(gw, xr);
                            break;
                        case "image":
                            LoadImage(gw, xr);
                            break;
                        case "option":
                            LoadDropdownOption((GlideDropdown)gw.Controls[gw.Controls.Count - 1], xr);
                            break;
                        case "passwordbox":
                            LoadPasswordBox(gw, xr);
                            break;
                        case "progressbar":
                            LoadProgressBar(gw, xr);
                            break;
                        case "radiobutton":
                            LoadRadioButton(gw, xr);
                            break;
                        case "slider":
                            LoadSlider(gw, xr);
                            break;
                        case "textblock":
                            LoadTextBlock(gw, xr);
                            break;
                        case "textbox":
                            LoadTextBox(gw, xr);
                            break;
                        case "window":
                            LoadWindow(gw, xr);
                            break;
                    }
                }
            }

            xr.Close();
            return true;
        }

        #endregion

        #region Save Methods

        private string BoolToString(bool b)
        {
            if (b)
                return "True";
            else
                return "False";
        }

        private string ColorToHex(Color c)
        {
            return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
        }

        private int GetFontId(GlideFonts f)
        {
            switch (f)
            {
                case GlideFonts.Droid_8:
                    return 0;
                case GlideFonts.Droid_9:
                    return 1;
                case GlideFonts.Droid_10:
                    return 2;
                case GlideFonts.Droid_11:
                    return 3;
                case GlideFonts.Droid_12:
                    return 4;
                case GlideFonts.Droid_14:
                    return 5;
                case GlideFonts.Droid_16:
                    return 6;
                case GlideFonts.Droid_18:
                    return 7;
                case GlideFonts.Droid_20:
                    return 8;
                default:
                    return 9;
            }
        }

        private string GetHorzAlign(StringAlignment sa)
        {
            switch (sa)
            {
                case StringAlignment.Center:
                    return "Center";
                case StringAlignment.Near:
                    return "Left";
                default:
                    return "Right";
            }
        }

        private string GetVertAlign(StringAlignment sa)
        {
            switch (sa)
            {
                case StringAlignment.Center:
                    return "Center";
                case StringAlignment.Near:
                    return "Top";
                default:
                    return "Bottom";
            }
        }

        private string SaveGlideButton(GlideButton obj)
        {
            return "<Button Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Text=\"" + obj.Text + "\" Font=\"" + GetFontId(obj.GFont) + "\" FontColor=\"" + ColorToHex(obj.FontColor) + "\" DisabledFontColor=\"" + 
                ColorToHex(obj.DisabledFontColor) + "\" TintColor=\"" + ColorToHex(obj.TintColor) + "\" TintAmount=\"" + obj.TintAmount + "\"/>";
        }

        private string SaveGlideCheckbox(GlideCheckbox obj)
        {
            return "<CheckBox Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Checked=\"" + BoolToString(obj.Checked) + "\"/>";
        }

        private string SaveGlideDataGrid(GlideDataGrid obj)
        {
            return "<DataGrid Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Font=\"" + GetFontId(obj.GFont) + "\" RowCount=\"" + obj.RowCount + "\" RowHeight=\"" + obj.RowHeight + "\" Draggable=\"" + BoolToString(obj.Draggable) +
                "\" TappableCells=\"" + BoolToString(obj.TappableCells) + "\" SortableHeaders=\"" + BoolToString(obj.SortableHeaders) + "\" ShowHeaders=\"" +
                BoolToString(obj.ShowHeaders) + "\" ShowScrollbar=\"" + BoolToString(obj.ShowScrollbar) + "\" ScrollbarWidth=\"" + obj.ScrollbarWidth +
                "\" HeadersBackColor=\"" + ColorToHex(obj.HeadersBackColor) + "\" HeadersFontColor=\"" + ColorToHex(obj.HeadersFontColor) + "\" ItemsBackColor=\"" +
                ColorToHex(obj.ItemsBackColor) + "\" ItemsAltBackColor=\"" + ColorToHex(obj.ItemsAltBackColor) + "\" ItemsFontColor=\"" + ColorToHex(obj.ItemsFontColor) +
                "\" SelectedItemBackColor=\"" + ColorToHex(obj.SelectedItemBackColor) + "\" SelectedItemFontColor=\"" + ColorToHex(obj.SelectedItemFontColor) + "\" GridColor=\"" + 
                ColorToHex(obj.GridColor) + "\" ScrollbarBackColor=\"" + ColorToHex(obj.ScrollbarBackColor) + "\" ScrollbarScrubberColor=\"" + ColorToHex(obj.ScrollbarScrubberColor) + "\"/>";
        }

        private string SaveGlideDropdown(GlideDropdown obj)
        {
            string sret = "<Dropdown Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Text=\"" + obj.Text + "\" Font=\"" + GetFontId(obj.GFont) + "\" FontColor=\"" + ColorToHex(obj.FontColor) + "\"";

            if (obj.Options == null || obj.Options.Length == 0)
                sret += " />";
            else
            {
                sret += ">\r\n";
                for (int i = 0; i < obj.Options.Length; i++)
                {
                    sret += "<Option Value=\"" + obj.Options[i].Value + "\">" + obj.Options[i].Text + "</Option>\r\n";
                }
                sret += "</Dropdown>";
            }

            return sret;
        }

        private string SaveGlideImage(GlideImage obj)
        {
            return "<Image Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" />";
        }

        private string SaveGlidePasswordBox(GlidePasswordBox obj)
        {
            return "<PasswordBox Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Font=\"" + GetFontId(obj.GFont) + "\" FontColor=\"" + ColorToHex(obj.FontColor) + "\" TextAlign=\"" + GetHorzAlign(obj.TextAlign) + "\" " +
                "Text=\"" + obj.Text + "\" />";
        }

        private string SaveGlideProgressBar(GlideProgressBar obj)
        {
            return "<ProgressBar Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Direction=\"" + ((obj.Direction == GlideDirections.Left) ? "Left" : "Right") + "\" MaxValue=\"" + obj.MaxValue + "\" Value=\"" + obj.Value + "\"/>";
        }

        private string SaveGlideRadioButton(GlideRadioButton obj)
        {
            return "<RadioButton Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Value=\"" + obj.Value + "\" Checked=\"" + BoolToString(obj.Checked) + "\" GroupName=\"" + obj.GroupName + "\" ShowBackground=\"" + BoolToString(obj.ShowBackground) +
                "\" Color=\"" + ColorToHex(obj.Color) + "\" OutlineColor=\"" + ColorToHex(obj.OutlineColor) + "\" SelectedColor=\"" + ColorToHex(obj.SelectedColor) +
                "\" SelectedOutlineColor=\"" + ColorToHex(obj.SelectedOutlineColor) + "\"/>";
        }

        private string SaveGlideSlider(GlideSlider obj)
        {
            return "<Slider Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Direction=\"" + ((obj.Direction == GlideOrientations.Horizontal) ? "horizontal" : "vertical") + "\" SnapInterval=\"" + obj.SnapInterval +
                "\" TickInterval=\"" + obj.TickInterval + "\" TickColor=\"" + ColorToHex(obj.TickColor) + "\" KnobSize=\"" + obj.KnobSize + "\" Minimum=\"" + obj.Minimum + 
                "\" Maximum=\"" + obj.Maximum + "\" Value=\"" + obj.Value + "\"/>";
        }

        private string SaveGlideTextBlock(GlideTextBlock obj)
        {
            return "<TextBlock Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Font=\"" + GetFontId(obj.GFont) + "\" FontColor=\"" + ColorToHex(obj.FontColor) + "\" TextAlign=\"" + GetHorzAlign(obj.TextAlign) + "\" " +
                "TextVerticalAlign=\"" + GetVertAlign(obj.TextVerticalAlign) + "\" BackColor=\"" + ColorToHex(obj.BackColor) + "\" " +
                "ShowBackColor=\"" + BoolToString(obj.ShowBackColor) + "\" Text=\"" + obj.Text + "\" />";
        }

        private string SaveGlideTextBox(GlideTextBox obj)
        {
            return "<TextBox Name=\"" + obj.Name + "\" X=\"" + obj.Left + "\" Y=\"" + obj.Top + "\" Width=\"" + obj.Width + "\" Height=\"" + obj.Height + "\" Alpha=\"255\" " +
                "Font=\"" + GetFontId(obj.GFont) + "\" FontColor=\"" + ColorToHex(obj.FontColor) + "\" TextAlign=\"" + GetHorzAlign(obj.TextAlign) + "\" " +
                "Text=\"" + obj.Text + "\" />";
        }

        #endregion

        #region Load Methods

        private GlideFonts GetGFont(string value)
        {
            switch (value)
            {
                case "0":
                    return GlideFonts.Droid_8;
                case "1":
                    return GlideFonts.Droid_9;
                case "2":
                    return GlideFonts.Droid_10;
                case "3":
                    return GlideFonts.Droid_11;
                case "4":
                    return GlideFonts.Droid_12;
                case "5":
                    return GlideFonts.Droid_14;
                case "6":
                    return GlideFonts.Droid_16;
                case "7":
                    return GlideFonts.Droid_18;
                case "8":
                    return GlideFonts.Droid_20;
                default:
                    return GlideFonts.Droid_24;
            }
        }

        private bool GetBool(string value)
        {
            if (value.ToLower() == "true")
                return true;
            return false;
        }

        private StringAlignment GetHorzAlign(string value)
        {
            switch (value.ToLower())
            {
                case "left":
                    return StringAlignment.Near;
                case "center":
                    return StringAlignment.Center;
                default:
                    return StringAlignment.Far;
            }
        }

        private StringAlignment GetVertAlign(string value)
        {
            switch (value.ToLower())
            {
                case "top":
                    return StringAlignment.Near;
                case "center":
                    return StringAlignment.Center;
                default:
                    return StringAlignment.Far;
            }
        }

        private void LoadButton(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideButton obj = new GlideButton();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Text = xr.GetAttribute("Text");
            obj.GFont = GetGFont(xr.GetAttribute("Font"));
            obj.FontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("FontColor"));
            obj.DisabledFontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("DisabledFontColor"));
            obj.TintColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("TintColor"));
            obj.TintAmount = byte.Parse(xr.GetAttribute("TintAmount"));
            gw.Controls.Add(obj);
        }

        private void LoadCheckbox(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideCheckbox obj = new GlideCheckbox();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Checked = GetBool(xr.GetAttribute("Checked"));

            gw.Controls.Add(obj);
        }

        private void LoadDataGrid(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideDataGrid obj = new GlideDataGrid();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.GFont = GetGFont(xr.GetAttribute("Font"));
            obj.RowCount = int.Parse(xr.GetAttribute("RowCount"));
            obj.RowHeight = int.Parse(xr.GetAttribute("RowHeight"));
            obj.Draggable = GetBool(xr.GetAttribute("Draggable"));
            obj.TappableCells = GetBool(xr.GetAttribute("TappableCells"));
            obj.SortableHeaders = GetBool(xr.GetAttribute("SortableHeaders"));
            obj.ShowHeaders = GetBool(xr.GetAttribute("ShowHeaders"));
            obj.ShowScrollbar = GetBool(xr.GetAttribute("ShowScrollbar"));
            obj.ScrollbarWidth = int.Parse(xr.GetAttribute("ScrollbarWidth"));
            obj.HeadersBackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("HeadersBackColor"));
            obj.HeadersFontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("HeadersFontColor"));
            obj.ItemsBackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("ItemsBackColor"));
            obj.ItemsAltBackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("ItemsAltBackColor"));
            obj.ItemsFontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("ItemsFontColor"));
            obj.SelectedItemBackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("SelectedItemBackColor"));
            obj.SelectedItemFontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("SelectedItemFontColor"));
            obj.GridColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("GridColor"));
            obj.ScrollbarBackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("ScrollbarBackColor"));
            obj.ScrollbarScrubberColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("ScrollbarScrubberColor"));

            gw.Controls.Add(obj);
        }

        private void LoadDropdown(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideDropdown obj = new GlideDropdown();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Text = xr.GetAttribute("Text");
            obj.GFont = GetGFont(xr.GetAttribute("Font"));
            obj.FontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("FontColor"));

            gw.Controls.Add(obj);
        }

        private void LoadDropdownOption(GlideDropdown dd, XmlReader xr)
        {
            GlideDropdownOption ddo = new GlideDropdownOption();
            ddo.Text = xr.GetAttribute("Text");
            ddo.Value = xr.GetAttribute("Value");

            if (dd.Options == null || dd.Options.Length == 0)
                dd.Options = new GlideDropdownOption[] { ddo };
            else
            {
                GlideDropdownOption[] tmp = new GlideDropdownOption[dd.Options.Length + 1];
                Array.Copy(dd.Options, tmp, dd.Options.Length);
                tmp[tmp.Length - 1] = ddo;
                dd.Options = tmp;
            }

        }

        private void LoadImage(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideImage obj = new GlideImage();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));

            gw.Controls.Add(obj);
        }

        private void LoadPasswordBox(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlidePasswordBox obj = new GlidePasswordBox();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Text = xr.GetAttribute("Text");
            obj.GFont = GetGFont(xr.GetAttribute("Font"));
            obj.FontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("FontColor"));
            obj.TextAlign = GetHorzAlign(xr.GetAttribute("TextAlign"));

            gw.Controls.Add(obj);
        }

        private void LoadProgressBar(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideProgressBar obj = new GlideProgressBar();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.MaxValue = int.Parse(xr.GetAttribute("MaxValue"));
            obj.Value = int.Parse(xr.GetAttribute("Value"));
            obj.Direction = ((xr.GetAttribute("Direction") == "Right") ? GlideDirections.Right : GlideDirections.Left);

            gw.Controls.Add(obj);
        }

        private void LoadRadioButton(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideRadioButton obj = new GlideRadioButton();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Value = xr.GetAttribute("Value");
            obj.Checked = GetBool(xr.GetAttribute("Checked"));
            obj.GroupName = xr.GetAttribute("GroupName");
            obj.ShowBackground = GetBool(xr.GetAttribute("ShowBackground"));
            obj.Color = (Color)cc.ConvertFromString("#" + xr.GetAttribute("Color"));
            obj.OutlineColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("OutlineColor"));
            obj.SelectedColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("SelectedColor"));
            obj.SelectedOutlineColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("SelectedOutlineColor"));

            gw.Controls.Add(obj);
        }

        private void LoadSlider(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideSlider obj = new GlideSlider();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Direction = ((xr.GetAttribute("Direction") == "horizontal") ? GlideOrientations.Horizontal : GlideOrientations.Vertical);
            obj.SnapInterval = int.Parse(xr.GetAttribute("SnapInterval"));
            obj.TickInterval = int.Parse(xr.GetAttribute("TickInterval"));
            obj.KnobSize = int.Parse(xr.GetAttribute("KnobSize"));
            obj.Minimum = int.Parse(xr.GetAttribute("Minimum"));
            obj.Maximum = int.Parse(xr.GetAttribute("Maximum"));
            obj.Value = int.Parse(xr.GetAttribute("Value"));
            obj.TickColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("TickColor"));

            gw.Controls.Add(obj);
        }

        private void LoadTextBlock(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideTextBlock obj = new GlideTextBlock();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Text = xr.GetAttribute("Text");
            obj.GFont = GetGFont(xr.GetAttribute("Font"));
            obj.FontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("FontColor"));
            obj.TextAlign = GetHorzAlign(xr.GetAttribute("TextAlign"));
            obj.TextVerticalAlign = GetHorzAlign(xr.GetAttribute("TextVerticalAlign"));
            obj.BackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("BackColor"));
            obj.ShowBackColor = GetBool(xr.GetAttribute("ShowBackColor"));

            gw.Controls.Add(obj);
        }

        private void LoadTextBox(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            GlideTextBox obj = new GlideTextBox();
            obj.Name = xr.GetAttribute("Name");
            obj.Left = int.Parse(xr.GetAttribute("X"));
            obj.Top = int.Parse(xr.GetAttribute("Y"));
            obj.Width = int.Parse(xr.GetAttribute("Width"));
            obj.Height = int.Parse(xr.GetAttribute("Height"));
            obj.Text = xr.GetAttribute("Text");
            obj.GFont = GetGFont(xr.GetAttribute("Font"));
            obj.FontColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("FontColor"));
            obj.TextAlign = GetHorzAlign(xr.GetAttribute("TextAlign"));

            gw.Controls.Add(obj);
        }

        private void LoadWindow(GlideWindow gw, XmlReader xr)
        {
            ColorConverter cc = new ColorConverter();
            gw.Name = xr.GetAttribute("Name");
            gw.Width = int.Parse(xr.GetAttribute("Width"));
            gw.Height = int.Parse(xr.GetAttribute("Height"));
            gw.BackColor = (Color)cc.ConvertFromString("#" + xr.GetAttribute("BackColor"));
        }

        #endregion

    }
}
